import { inject, observer } from 'mobx-react';
import { WeaTools, WeaProgress, WeaDialog } from 'ecCom';
import { Button } from 'antd';
import BaseList from '../BaseList';
import ReportDetail from './ReportDetail';
import ReportTimeSectionForm from './ReportTimeSectionForm';
import * as Bctt from '../../constants/BaseListConstant';
import { toJS } from 'mobx';
import { WeaLocaleProvider } from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;

@inject('chanceReportStore')
@inject('chanceReportDetailStore')
@inject('chanceReportTimeSectionFormStore')
@observer
class Report extends BaseList {
  getStore() {
    return this.props.chanceReportStore;
  }

  reRenderColumns(columns) {
    const store = this.getStore();
    const { type } = store.routerParams;
    if (type != 'report') {
      columns.forEach((c) => {
        if (c.dataIndex == 'resultcount') {
          // 占比
          c.render = function (text, record) {
            if (!record.hasOwnProperty('_stat')) {
              return <span className="wea-crmReport-progressStyle"><WeaProgress ecId={`${this && this.props && this.props.ecId || ''}_WeaProgress@zs5d2y`} percent={record.resultcountspan} /></span>;
            }
            return <div />;
          };
        } else if (c.dataIndex == 'resultcount_n') {
          // 计数
          c.render = function (text, record) {
            let keyWord = '';
            let keyWordValue = '';
            if (type == 'reportEndStatus') {
              keyWord = 'endStatus';
              keyWordValue = record.endtatusid;
            } else if (type == 'reportSellStatus') {
              keyWord = 'sellstatusid';
              keyWordValue = record.sellstatusid;
            } else if (type == 'reportSuccessFactor') {
              keyWord = 'sufactor';
              keyWordValue = record.sufactor;
            } else if (type == 'reportFailFactor') {
              keyWord = 'defactor';
              keyWordValue = record.defactor;
            } else if (type == 'reportTimeSection') {
              keyWord = 'range1';
              keyWordValue = record.range1;
            }
            if (!record.hasOwnProperty('_stat')) {
              const params = { ...store.searchConditionForm.getFormParams() };
              for (const item in params) {
                if (params[item] == '') {
                  delete params[item];
                }
              }
              params.type = 'report';
              if (keyWord == 'range1') {
                // 时间分布
                const arr = keyWordValue.split('--');
                params.endStatus = '0';// 进行中的商机
                params.sellPlanDate_selectType = '6';// 指定日期区间
                params.sellPlanDate_fromDate = arr[0];
                params.sellPlanDate_toDate = arr.length >= 2 ? arr[1] : '';
              } else {
                params[keyWord] = keyWordValue;
              }
              return <a onClick={() => { store.showChanceDialog(true, params); }}>{record.resultcount_n}</a>;
            }
            return <span>{record.resultcount_n}</span>;
          };
        }
      });
    }
    return columns;
  }

  getTabSearchTypes(store) {
    const type = store.routerParams.type;
    const arr = [];
    if (type == 'report') {
      arr.push(Bctt.TAB_SEARCH_TYPE_BASE);
      arr.push(Bctt.TAB_SEARCH_TYPE_ADVANCED);
    } else if (type != 'reportEndStatus') {
      arr.push(Bctt.TAB_SEARCH_TYPE_ADVANCED);
    }
    return arr;
  }

  renderAppend(store) {
    const type = store.routerParams.type;
    const arr = [];
    if (type != 'report') {
      const { chanceDialogTile, chanceDialogShow, showChanceDialog, chanceDialogParams } = store;
      if (chanceDialogShow) {
        const params = toJS(chanceDialogParams);
        const buttons = [];
        buttons.push(
          <Button
            ecId={`${this && this.props && this.props.ecId || ''}_Button@gp51be@close`}
            onClick={() => {
              this.closeChanceDialog(store);
            }}
          >{getLabel(309, '关闭')}</Button>,
        );
        arr.push(<WeaDialog
          ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@ijpwti@close`}
          icon="icon-coms-crm"
          iconBgcolor="#96358a"
          style={{ width: 1000, height: 500 }}
          visible={chanceDialogShow}
          title={chanceDialogTile}
          buttons={buttons}
          onCancel={() => {
            this.closeChanceDialog(store);
          }}
        >
          <ReportDetail ecId={`${this && this.props && this.props.ecId || ''}_ReportDetail@4euedh@rd`} params={params} />
        </WeaDialog>);
      }
    }
    if (type == 'reportTimeSection') {
      const { customDialogTile, customDialogShow, showCustomDialog } = store;
      if (customDialogShow) {
        const buttons = this.props.chanceReportTimeSectionFormStore.getTopButtons();
        buttons.push(
          <Button
            ecId={`${this && this.props && this.props.ecId || ''}_Button@r4i7wu@close`}
            onClick={() => {
              this.closeCustomDialog(store);
            }}
          >{getLabel(309, '关闭')}</Button>,
        );
        arr.push(<WeaDialog
          ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@k34n8a@close`}
          icon="icon-coms-crm"
          iconBgcolor="#96358a"
          style={{ width: 600, height: 250 }}
          visible={customDialogShow}
          title={customDialogTile}
          buttons={buttons}
          onCancel={() => {
            this.closeCustomDialog(store);
          }}
        >
          <ReportTimeSectionForm ecId={`${this && this.props && this.props.ecId || ''}_ReportTimeSectionForm@9eidik@rtsf`} parentStore={store} />
        </WeaDialog>);
      }
    }
    return arr;
  }

  closeChanceDialog(store) {
    store.showChanceDialog(false);
  }

  closeCustomDialog(store) {
    store.showCustomDialog(false);
    this.props.chanceReportTimeSectionFormStore.componentWillUnmount();
  }
}

export default WeaTools.tryCatch(React, props => <WeaErrorPage ecId={`${this && this.props && this.props.ecId || ''}_WeaErrorPage@lf0z7d`} msg={props.error ? props.error : getLabel(129417, '服务器运行出错，请联系系统管理员！')} />, { error: '' })(Report);
